package edu.wright.ceg4980.year2013.keyboard;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.util.Collection;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;


@SuppressWarnings("serial")
public class EnterPasswordScreen extends JPanel {
	private JTextField tf1, tf2;
	
	private JScrollPane errorView;
	
	private DefaultListModel<String> errorListModel;
	
	public EnterPasswordScreen() {
		super(new BorderLayout(0, 20));
		setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		
//TODO add a message that tells the user NOT to enter a password they are currently using or will use in the future
		
		JLabel firstLbl = new JLabel("Please enter a test password:");
		tf1 = new JTextField(15);
		
		JLabel secondLbl = new JLabel("Please re-enter test password:");
		tf2 = new JTextField(15);
		
		errorListModel = new DefaultListModel<String>();
		JList<String> errorList = new JList<String>(errorListModel);
		errorList.setEnabled(false);
		errorList.setBackground(getBackground());
		
		errorView = new JScrollPane(errorList);
		errorView.setBackground(getBackground());
		errorView.setBorder(BorderFactory.createLineBorder(Color.RED));
		errorView.setVisible(false);
		
		JPanel pswdPanel = new JPanel(new GridLayout(0, 1, 0, 5));
		
		add(pswdPanel, BorderLayout.NORTH);
			pswdPanel.add(firstLbl);
			pswdPanel.add(tf1);
			pswdPanel.add(secondLbl);
			pswdPanel.add(tf2);
		add(errorView, BorderLayout.CENTER);
	}
	
	public void clear() {
		tf1.setText("");
		tf2.setText("");
	}
	
	public String getPassword1() {
		return tf1.getText();
	}
	
	public String getPassword2() {
		return tf2.getText();
	}
	
	public void setErrorList(Collection<String> errors) {
		errorListModel.clear();
		if (errors.isEmpty()) {
			errorView.setVisible(false);
		} else {
			for (String error : errors) {
				errorListModel.addElement(error);
			}
			errorView.setVisible(true);
			EnterPasswordScreen.this.revalidate();
			EnterPasswordScreen.this.repaint();
		}
	}
}
